# === load relevant libraries
library(data.table)
library(ggplot2)
library(DescTools)
library(plm)
library(lfe)
library(dplyr)
library(sandwich)

rm(list = ls())

# get style returns and flows
data = readRDS('input_data/morningstar_style_data.RDS')
data = data[, list(yyyymm, category, ret, flow)]
data = as.data.table(melt(data, id.vars = c('yyyymm','category')))
names(data)[3] = c('var')

# get subsequent 36 months
tmp = unique(data[, list(yyyymm)])
tmp[, idx := 1:nrow(tmp)]
data = merge(data, tmp); rm(tmp)
out = data.table()
for (i in 0:36){
  out = rbind(out, data[, list(idx = idx-i, hor = i, category, var, value)])
}
out = merge(out, unique(data[, list(idx, yyyymm)]), by = 'idx')
out[, idx := NULL]
data = copy(out); rm(out)

# get sorting based on different definitions of past rating changes
sortData = readRDS('input_data/style_ratings_alternative_specifications.RDS')
sortData = as.data.table(melt(sortData, id.vars = c('yyyymm','category')))
names(sortData)[3:4] = c('sortVar','rating')
sortData[, sortVar := as.character(sortVar)]
sortData[, sortVar := as.integer(substr(sortVar, 4, nchar(sortVar)-1))]
sortData = sortData[order(yyyymm, sortVar, rating)]
for (thisM in unique(sortData[, yyyymm])){
  for (thisVar in unique(sortData[, sortVar])){
    sortData[(yyyymm == thisM) & (sortVar == thisVar), bin := 1:9]
  }
}
rm(thisM, thisVar)
sortData[, rating := NULL]

# for each possible combination, get cumulative paths
vars = c('ret','flow')
sortVars = unique(sortData[, sortVar])
tmp = data.table()
for (thisVar in vars){
  for (thisSortVar in sortVars){
    tmp = rbind(tmp, data.table(var = thisVar, sortVar = thisSortVar))
  }
}
rm(vars, sortVars, thisVar, thisSortVar)
tmp[, idx := 1:nrow(tmp)]

# function to get one set of cumulative paths
p.getOne = function(this){
  out = copy(data[var == tmp[idx == this, var]])
  tt = copy(sortData[sortVar == tmp[idx == this, sortVar]])
  out = merge(out, tt, by = c('yyyymm','category')); rm(tt)
  
  # get paths
  out[, period := ifelse(yyyymm > 200206, '2_after 2002', '1_before 2002')]
  out = out[, list(value = mean(value)), list(bin, hor, period)]
  out = out[order(hor)]
  
  # get top minus bottom
  out = merge(out[bin == 9, list(hor, period, top = value)], 
              out[bin == 1, list(hor, period, bottom = value)], by = c('hor','period'))
  out = out[, list(hor, period, idx = this, estimate = top - bottom)]
  
  # get cumulative paths
  out = out[order(hor)]
  for (thisP in unique(out[, period])){
    out[period == thisP, estimate := cumsum(estimate)]
  }
  
  return(out)
  
}

data = Reduce(rbind, lapply(tmp[, idx], p.getOne))
data = merge(data, tmp, by = 'idx'); rm(tmp, sortData)
data[, idx := NULL]

# append bootstrapped standard errors
tmp = readRDS('input_data/style_price_pressure_bootstraps_alternative_ratings_specification.RDS')
data = merge(data, tmp, by = c('hor','var','period','sortVar')); rm(tmp)
data_bk = copy(data)

# ==== plot Figure A3
data = copy(data_bk[var == 'flow'])
yy = c(-.1, .3)

# (a): 3-month rating change
ggplot(data[sortVar == 3], aes(x = hor, y = estimate, fill = period)) + geom_line(aes(color = period), lwd = 1) + 
  geom_ribbon(aes(ymin = estimate - 1.96*se, ymax = estimate + 1.96*se), alpha = .2) + theme_classic() + 
  theme(legend.title = element_blank(), legend.position = c(.25, .9)) + coord_cartesian(ylim = yy) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + 
  labs(x = 'Months', y = 'Cumulative flow: top minus bottom style') + geom_hline(yintercept = 0, lty = 3)

# (b): 6-month rating change
ggplot(data[sortVar == 6], aes(x = hor, y = estimate, fill = period)) + geom_line(aes(color = period), lwd = 1) + 
  geom_ribbon(aes(ymin = estimate - 1.96*se, ymax = estimate + 1.96*se), alpha = .2) + theme_classic() + 
  theme(legend.title = element_blank(), legend.position = c(.25, .9)) + coord_cartesian(ylim = yy) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + 
  labs(x = 'Months', y = 'Cumulative flow: top minus bottom style') + geom_hline(yintercept = 0, lty = 3)

# (c): 9-month rating change
ggplot(data[sortVar == 9], aes(x = hor, y = estimate, fill = period)) + geom_line(aes(color = period), lwd = 1) + 
  geom_ribbon(aes(ymin = estimate - 1.96*se, ymax = estimate + 1.96*se), alpha = .2) + theme_classic() + 
  theme(legend.title = element_blank(), legend.position = c(.25, .9)) + coord_cartesian(ylim = yy) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + 
  labs(x = 'Months', y = 'Cumulative flow: top minus bottom style') + geom_hline(yintercept = 0, lty = 3)

# (d): 12-month rating change
ggplot(data[sortVar == 12], aes(x = hor, y = estimate, fill = period)) + geom_line(aes(color = period), lwd = 1) + 
  geom_ribbon(aes(ymin = estimate - 1.96*se, ymax = estimate + 1.96*se), alpha = .2) + theme_classic() + 
  theme(legend.title = element_blank(), legend.position = c(.25, .9)) + coord_cartesian(ylim = yy) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + 
  labs(x = 'Months', y = 'Cumulative flow: top minus bottom style') + geom_hline(yintercept = 0, lty = 3)


# ==== plot Figure A4
data = copy(data_bk[var == 'ret'])
yy = c(-.15, .2)

# (a): 3-month rating change
ggplot(data[sortVar == 3], aes(x = hor, y = estimate, fill = period)) + geom_line(aes(color = period), lwd = 1) + 
  geom_ribbon(aes(ymin = estimate - 1.96*se, ymax = estimate + 1.96*se), alpha = .2) + theme_classic() + 
  theme(legend.title = element_blank(), legend.position = c(.25, .9)) + coord_cartesian(ylim = yy) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + 
  labs(x = 'Months', y = 'Cumulative return: top minus bottom style') + geom_hline(yintercept = 0, lty = 3)

# (b): 6-month rating change
ggplot(data[sortVar == 6], aes(x = hor, y = estimate, fill = period)) + geom_line(aes(color = period), lwd = 1) + 
  geom_ribbon(aes(ymin = estimate - 1.96*se, ymax = estimate + 1.96*se), alpha = .2) + theme_classic() + 
  theme(legend.title = element_blank(), legend.position = c(.25, .9)) + coord_cartesian(ylim = yy) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + 
  labs(x = 'Months', y = 'Cumulative return: top minus bottom style') + geom_hline(yintercept = 0, lty = 3)

# (c): 9-month rating change
ggplot(data[sortVar == 9], aes(x = hor, y = estimate, fill = period)) + geom_line(aes(color = period), lwd = 1) + 
  geom_ribbon(aes(ymin = estimate - 1.96*se, ymax = estimate + 1.96*se), alpha = .2) + theme_classic() + 
  theme(legend.title = element_blank(), legend.position = c(.25, .9)) + coord_cartesian(ylim = yy) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + 
  labs(x = 'Months', y = 'Cumulative return: top minus bottom style') + geom_hline(yintercept = 0, lty = 3)

# (d): 12-month rating change
ggplot(data[sortVar == 12], aes(x = hor, y = estimate, fill = period)) + geom_line(aes(color = period), lwd = 1) + 
  geom_ribbon(aes(ymin = estimate - 1.96*se, ymax = estimate + 1.96*se), alpha = .2) + theme_classic() + 
  theme(legend.title = element_blank(), legend.position = c(.25, .9)) + coord_cartesian(ylim = yy) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + 
  labs(x = 'Months', y = 'Cumulative return: top minus bottom style') + geom_hline(yintercept = 0, lty = 3)

